import storeInfoModel from "../model/storeInfoModel";
import consumerRecoderModel from "../model/consumerRecoderModel";
import * as moment from 'moment';

class StoreInfoService{
    constructor(){}

    //根据员工id查询门店信息
    async checkStoreInfoByEmployeeId(employeeId:string){
        let json:any=[];
        let codeList=await consumerRecoderModel.distinct("consumerCode",{userid:employeeId,comment:1});
        let data=await storeInfoModel.find({consumerCode:{$in:codeList}})
        if(data.length){
            for (let index_ = 0; index_ < data.length; index_++) {
                let element = data[index_];
                json.push({
                    id:element._id,//id
                    code:element.storeCode,//门店编码
                    name:element.storeName,//门店名称
                    channel:element.channel,//渠道
                    store_classification:element.store_classification,//门店类型
                    contact_name:element.contact_name,//联系人姓名
                    phone:element.phone, //电话
                    consumerCode:element.consumerCode,//客户编码
                    consumerName:element.consumerName,//客户名称
                    city:element.city,//城市
                    cityType:element.cityType,
                    store_address:element.store_address,//门店地址
                    iT_Mall_name:element.iT_Mall_name,//IT Mall name
                    iT_Mall_address:element.iT_Mall_address
                });
            }
        }
        return json;   
    }

    //查询所有门店信息
    async checkAllStore(){
        let json:any=[];
        let data=await storeInfoModel.find({});
        if(data.length){
            for (let index = 0; index < data.length; index++) {
                let element = data[index];//element=consumerInfo
                json.push({
                    id:element._id,//id
                    code:element.storeCode,//门店编码
                    name:element.storeName,//门店名称
                    channel:element.channel,//渠道
                    store_classification:element.store_classification,//门店类型
                    contact_name:element.contact_name,//联系人姓名
                    phone:element.phone, //电话
                    consumerCode:element.consumerCode,//客户编码
                    consumerName:element.consumerName,//客户名称
                    city:element.city,//城市
                    cityType:element.cityType,
                    store_address:element.store_address,//门店地址
                    iT_Mall_name:element.iT_Mall_name,//IT Mall name
                    iT_Mall_address:element.iT_Mall_address
                })
            }
        }
        return json;
    }

    //添加门店信息
    async addStore(res:any){
        // console.log(res);
        
        let a=await storeInfoModel.create({
            sourceId: res.storeCode,
          
            storeCode:res.storeCode,//门店编码
            storeName:res.storeName,//门店名称
            channel:res.channel,//渠道
            store_classification:res.store_classification,//门店分类
           
            contact_name:res.contact_name,//联系人姓名
            phone:res.phone,//电话
            consumerCode:res.consumerCode,//客户编码
            consumerName:res.consumerName,//客户名称
            city:res.city,//城市
            cityType:res.cityType,
            store_address:res.store_address,//门店地址
            iT_Mall_name:res.iT_Mall_name,//IT Mall name
            iT_Mall_address:res.iT_Mall_address,   
            date:moment().format('YYYY-MM-DD')             
        });
        return a;
    }

    //删除客户信息
    async deleteStore(idList:string){
        // console.log('要删除的门店的id:'+idList);
        for (let index = 0; index < idList.length; index++) {
            let element = idList[index];//element=id
            let b=await storeInfoModel.findByIdAndRemove({_id:element});  
            // console.log(b,'b_b_b_b_b_b_b_b_',index)
        }
        console.log('删除客户信息结束!!!!')
    }

    //通过门店等级或门店渠道查询门店信息----筛选功能
    //参数:type:字符串   
    async checkStoreByGradeOrChannel(type:string){
        // console.log('type:'+type);
        let json:any=[];
        if(type=='店铺等级'||type=='店铺渠道'){
            json=await this.checkAllStore();
        }else{
            let data=await storeInfoModel.find({"$or":[{channel:type},{store_classification:type}]})
            for (let index = 0; index < data.length; index++) {
                let element = data[index];
                json.push({
                    id:element._id,//id
                    code:element.storeCode,//门店编码
                    name:element.storeName,//门店名称
                    channel:element.channel,//渠道
                    store_classification:element.store_classification,//门店分类
                    contact_name:element.contact_name,//联系人姓名
                    phone:element.phone, //电话
                    consumerCode:element.consumerCode,//客户编码
                    consumerName:element.consumerName,//客户名称
                    city:element.city,//城市
                    cityType:element.cityType,
                    store_address:element.store_address,//门店地址
                    iT_Mall_name:element.iT_Mall_name,//IT Mall name
                    iT_Mall_address:element.iT_Mall_address
                });
            }
        }
        // console.log(json);
        return json;
    } 

    //搜索功能----模糊搜索与筛选功能一起查询数据
    async fuzzyCheckStore(character:string,type:string){
        // console.log('type:'+type+'-----模糊搜索字段:'+character);
        let json:any=[];
        let data_:any[]=[]
        if(character&&type==null){
            data_=await storeInfoModel.find({storeName:{$regex:character}});
        }
        if(type&&character==null){
            data_=await storeInfoModel.find({"$or":[{channel:type},{store_classification:type}]});
            // console.log(data_,'type<================')
        }else if(character&&type){
            data_=await storeInfoModel.find({"$or":[{channel:type},{store_classification:type},{}],storeName:{$regex:character}});
        }else{
            data_=await storeInfoModel.find({});
        }
        if(data_.length>0){
            for (let index = 0; index < data_.length; index++) {
                let data = data_[index];
                json.push({
                    id:data._id,//id
                    code:data.storeCode,//门店编码
                    name:data.storeName,//门店名称
                    channel:data.channel,//渠道
                    store_classification:data.store_classification,//门店分类
                    contact_name:data.contact_name,//联系人姓名
                    phone:data.phone, //电话
                    consumerCode:data.consumerCode,//客户编码
                    consumerName:data.consumerName,//客户名称
                    city:data.city,//城市
                    store_address:data.store_address,//门店地址
                    iT_Mall_name:data.iT_Mall_name,//IT Mall name
                    iT_Mall_address:data.iT_Mall_address
                });
            }
        }
        return json;   
    }

    //获取全部店铺渠道
    async getStoreChannel(){
        let data=await storeInfoModel.distinct("channel",{})
        return data;
    }
    //获取全部店铺渠道
    async getStoreGrade(){
        let data=await storeInfoModel.distinct("store_classification",{})
        return data;
    }


    
  


}
let storeInfo=new StoreInfoService();
export default storeInfo;